{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "9d7a6152-c923-4007-b708-80715108257f",
   "metadata": {},
   "source": [
    "## 打开指定shp数据"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "06981b5f-4558-4124-be63-449c218d6c90",
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "d:\\GitHub\\GeoBigDataPy313\\.venv\\Lib\\site-packages\\osgeo\\ogr.py:601: FutureWarning: Neither ogr.UseExceptions() nor ogr.DontUseExceptions() has been explicitly called. In GDAL 4.0, exceptions will be enabled by default.\n",
      "  warnings.warn(\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "19"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# 导入包\n",
    "from osgeo import ogr\n",
    "import os\n",
    "\n",
    "#设置数据路径\n",
    "shapefile = r\"data/JianghanPlain_C.shp\"\n",
    "# 指定驱动\n",
    "driver = ogr.GetDriverByName(\"ESRI Shapefile\")\n",
    "# 数据源打开数据\n",
    "dataSource = driver.Open(shapefile, 0)\n",
    "# 获取图层\n",
    "layer = dataSource.GetLayer()\n",
    "# 获取图层中要素的个数\n",
    "layer.GetFeatureCount()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "42b84a47-9de2-4781-8834-8521c0d81a69",
   "metadata": {},
   "source": [
    "## 查看图层范围"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "4c1481bc-de9d-4b14-8542-4058a95f3703",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4594025.000277772, 4873615.283729422, 3139661.1292879847, 3410355.3620396117)"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "layer.GetExtent()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "ad4856ec-405c-46dc-84b2-c577089605cb",
   "metadata": {},
   "source": [
    "## 查看空间参考"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "097cb58e-281b-498b-bc57-96bf60b8d5c0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "<osgeo.osr.SpatialReference; proxy of <Swig Object of type 'OSRSpatialReferenceShadow *' at 0x0000018ED8112D60> >"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "layer.GetSpatialRef()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "75d0aa98-c5da-4ee8-bfce-a8544b22cb17",
   "metadata": {},
   "source": [
    "## 图层的属性"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "90fb90e8-8008-4449-8e54-012cadc14d7c",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "NAME 60 4 0\n",
      "PAC 9 0 0\n",
      "Prov_ID 4 0 0\n",
      "Zone_ID 4 0 0\n",
      "DISTCODE 16 2 6\n",
      "County_Nam 254 4 0\n",
      "County_N_1 254 4 0\n",
      "City_Name 254 4 0\n",
      "City_Name1 254 4 0\n",
      "Province_N 254 4 0\n",
      "Province_1 254 4 0\n",
      "C2000 16 2 6\n",
      "C2001 16 2 6\n",
      "C2002 16 2 6\n",
      "C2003 16 2 6\n",
      "C2004 16 2 6\n",
      "C2005 16 2 6\n",
      "C2006 16 2 6\n",
      "C2007 16 2 6\n",
      "C2008 16 2 6\n",
      "C2009 16 2 6\n",
      "C2010 16 2 6\n",
      "C2011 16 2 6\n",
      "C2012 16 2 6\n",
      "C2013 16 2 6\n",
      "C2014 16 2 6\n",
      "C2015 16 2 6\n",
      "C2016 16 2 6\n",
      "C2017 16 2 6\n"
     ]
    }
   ],
   "source": [
    "layerdef = layer.GetLayerDefn()\n",
    "for i in range(layerdef.GetFieldCount()):\n",
    "     defn = layerdef.GetFieldDefn(i)\n",
    "     print(defn.GetName(),defn.GetWidth(),defn.GetType(),defn.GetPrecision())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "f1f7fc58-431e-416e-9746-3da6137d1a7b",
   "metadata": {},
   "source": [
    "## 第1个要素的属性字段"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "9d4cbb1d-3403-4d45-ab71-00d3492294d0",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['NAME',\n",
       " 'PAC',\n",
       " 'Prov_ID',\n",
       " 'Zone_ID',\n",
       " 'DISTCODE',\n",
       " 'County_Nam',\n",
       " 'County_N_1',\n",
       " 'City_Name',\n",
       " 'City_Name1',\n",
       " 'Province_N',\n",
       " 'Province_1',\n",
       " 'C2000',\n",
       " 'C2001',\n",
       " 'C2002',\n",
       " 'C2003',\n",
       " 'C2004',\n",
       " 'C2005',\n",
       " 'C2006',\n",
       " 'C2007',\n",
       " 'C2008',\n",
       " 'C2009',\n",
       " 'C2010',\n",
       " 'C2011',\n",
       " 'C2012',\n",
       " 'C2013',\n",
       " 'C2014',\n",
       " 'C2015',\n",
       " 'C2016',\n",
       " 'C2017']"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "layer.ResetReading()\n",
    "feat=layer.GetFeature(0)\n",
    "feat.keys()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2f06d2d3-fb6d-4b3a-b38c-2216d3e4030b",
   "metadata": {},
   "source": [
    "## 获取指定要素中指定字段的值"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "a28dd5d6-ef80-490c-b94c-222f8a35aa0f",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "610.630918"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "feat.GetField('C2017')"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "7be4fc16-a576-486d-b97b-53c62cd2152b",
   "metadata": {},
   "source": [
    "## 指定1个要素所有属性值进行遍历"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "6b4b5d6b-7c48-43ab-b086-8aae811513dd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "京山县\n",
      "420821\n",
      "42\n",
      "4208\n",
      "420821.0\n",
      "京山县\n",
      "Jingshan County\n",
      "荆门市\n",
      "Jingmen City\n",
      "湖北省\n",
      "Hubei Province\n",
      "461.447655\n",
      "391.006619\n",
      "609.368555\n",
      "567.734145\n",
      "578.683321\n",
      "446.303208\n",
      "516.119834\n",
      "559.565405\n",
      "572.879318\n",
      "535.8353\n",
      "583.783653\n",
      "508.470179\n",
      "527.81002\n",
      "569.875792\n",
      "611.2581\n",
      "633.622914\n",
      "592.300526\n",
      "610.630918\n"
     ]
    }
   ],
   "source": [
    "for i in range(feat.GetFieldCount()):\n",
    "    print(feat.GetField(i))"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "879121c4-917a-4d30-aecf-6c81d4ba5472",
   "metadata": {},
   "source": [
    "## 要素的几何形状(Geometry)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "2e8342fa-83ce-44d2-9812-305b8e6ca95b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "'MULTIPOLYGON'"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "geom = feat.GetGeometryRef()\n",
    "geom.GetGeometryName()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "2345832d-2020-4e50-abbb-c0575beffe00",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "2"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "geom.GetGeometryCount()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "005653be-05cb-4846-b9fc-9474f3673cd0",
   "metadata": {},
   "source": [
    "## 关闭数据源，释放内存，需要时使用"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "aa122b0e-08b5-491f-b436-40e2f9f7a85e",
   "metadata": {},
   "outputs": [],
   "source": [
    "dataSource.Destroy()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5f235929-c00c-4aae-9341-42168af40b88",
   "metadata": {},
   "source": [
    "## 空间过滤"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "aa49a2d6-2e99-40aa-a737-f3ad65751422",
   "metadata": {},
   "source": [
    "#### 如果说按照属性筛选要素是带有数据库特征的话，那么，根据空间位置的筛选就是纯GIS了。在OGR中，使用了Spatial filters（空间过滤）这一术语表征这一功能。OGR提供的空间过滤功能有两种：\n",
    "\n",
    "- 一种是SetSpatialFilter(geom)—过滤某一类型的Feature，如参数中的Polygon，效用就是选出Layer中的所有Polygon覆盖的要素（注意，只要相交即可，不必完全包含）。\n",
    "- 还有 SetSpatialFilterRect(<minx>, <miny>, <maxx>, <maxy>) 参数，可输入四个坐标选中矩形内的要素。"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "42942df6-1d17-4884-b9f1-07f1207fc545",
   "metadata": {},
   "source": [
    "### 图层生成shp数据的函数 "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "ad78a02e-b801-44e3-b05a-84a8780d32bf",
   "metadata": {},
   "outputs": [],
   "source": [
    "# 导入包\n",
    "from osgeo import ogr\n",
    "import os\n",
    "def create_shp_by_layer(outputfile, layer):\n",
    "     if os.access(outputfile, os.F_OK):\n",
    "         driver.DeleteDataSource(outputfile)\n",
    "     newds = driver. CreateDataSource ( outputfile )\n",
    "     pt_layer = newds.CopyLayer ( layer, '')\n",
    "     newds.Destroy ()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "159e5c66-db62-4e2f-9e8f-674cdb9a50d5",
   "metadata": {},
   "source": [
    "### 打开shp数据及图层"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "8ff53cca-38cd-4199-8f02-f170eeb37c5e",
   "metadata": {},
   "outputs": [],
   "source": [
    "driver = ogr.GetDriverByName(\"ESRI Shapefile\")\n",
    "JHPlain_C_shp = r\"data/JianghanPlain_C.shp\"\n",
    "JH_Filter_shp = r\"data/JH_SpatialFilter.shp\"\n",
    "JHPlain_C_ds = ogr.Open(JHPlain_C_shp)\n",
    "JH_Filter_ds = ogr.Open(JH_Filter_shp)\n",
    "JHPlain_C_layer = JHPlain_C_ds.GetLayer(0)\n",
    "JH_Filter_layer = JH_Filter_ds.GetLayer(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "3a1050ae-6cd4-4478-90ee-26cbcd48db23",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "19"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "JHPlain_C_layer.GetFeatureCount()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "f45e016d-d122-4901-a090-9280a5f01893",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4594025.000277772, 4873615.283729422, 3139661.1292879847, 3410355.3620396117)"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "JHPlain_C_layer.GetExtent()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "c8802018-0909-48ed-9d60-15dc6517a773",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "1"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "JH_Filter_layer.GetFeatureCount()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "ee2eff75-de4e-401c-8b17-39cbdd677829",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(4707539.834705172, 4772160.682241217, 3234634.273425484, 3271461.2080427934)"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "JH_Filter_layer.GetExtent()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "98757631-f926-4e1c-ae85-9282a41df32e",
   "metadata": {},
   "source": [
    "### 过滤图层的空间范围获取"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "60ab039e-b468-4851-a7c5-d0102b49574c",
   "metadata": {},
   "outputs": [],
   "source": [
    "JH_Filter_feats = JH_Filter_layer.GetNextFeature()\n",
    "JH_Filter_poly = JH_Filter_feats.GetGeometryRef()\n",
    "JHPlain_C_layer.SetSpatialFilter(JH_Filter_poly)\n",
    "out_shp = r'data/JHPlain_C_Filter.shp'\n",
    "create_shp_by_layer(out_shp, JHPlain_C_layer)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "96bc274b-bb46-475e-b418-8549d866315b",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "19"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "JHPlain_C_layer.SetSpatialFilter(None)\n",
    "JHPlain_C_layer.GetFeatureCount()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "id": "9d839556-d742-4ffc-b302-cc68bd816d71",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "JHPlain_C_layer.SetSpatialFilterRect(4707539, 4772160, 3234634, 3271461)\n",
    "print(JHPlain_C_layer.GetFeatureCount())"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "2b62cfdd-5de5-4c63-8eee-91c20babab0a",
   "metadata": {},
   "source": [
    "## 在OGR中使用SQL语句进行查询"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "01e6c99d-41e4-42ec-b2f7-b451c62231f4",
   "metadata": {},
   "source": [
    "## 根据属性条件选择要素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "d3a86730-8a9c-45c6-8889-8b21cba3c295",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "19\n"
     ]
    }
   ],
   "source": [
    "driver = ogr.GetDriverByName(\"ESRI Shapefile\")\n",
    "shp = r\"data/JianghanPlain_C.shp\"\n",
    "ds = ogr.Open(shp)\n",
    "layer = ds.GetLayer(0)\n",
    "Feature_Count = layer.GetFeatureCount()\n",
    "print(Feature_Count)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "7958f67a-865b-4a26-b2eb-8d85f59ac0e4",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "['NAME',\n",
       " 'PAC',\n",
       " 'Prov_ID',\n",
       " 'Zone_ID',\n",
       " 'DISTCODE',\n",
       " 'County_Nam',\n",
       " 'County_N_1',\n",
       " 'City_Name',\n",
       " 'City_Name1',\n",
       " 'Province_N',\n",
       " 'Province_1',\n",
       " 'C2000',\n",
       " 'C2001',\n",
       " 'C2002',\n",
       " 'C2003',\n",
       " 'C2004',\n",
       " 'C2005',\n",
       " 'C2006',\n",
       " 'C2007',\n",
       " 'C2008',\n",
       " 'C2009',\n",
       " 'C2010',\n",
       " 'C2011',\n",
       " 'C2012',\n",
       " 'C2013',\n",
       " 'C2014',\n",
       " 'C2015',\n",
       " 'C2016',\n",
       " 'C2017']"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "layer.ResetReading()\n",
    "feat=layer.GetFeature(0)\n",
    "feat.keys()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "c28f056b-401f-4a5e-893d-571f81f394cf",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "610.630918"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "feat.GetField('C2017')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "65e96f0e-12ea-4f9e-bfb6-9d4bbc79d1cc",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "5\n"
     ]
    }
   ],
   "source": [
    "layer.SetAttributeFilter(\"C2017 > 400\")\n",
    "lyr_count = layer.GetFeatureCount()\n",
    "print(lyr_count)\n",
    "out_shp = r'data/JH_SQL_Attribute_Filter.shp'\n",
    "create_shp_by_layer(out_shp, layer)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c441bbdf-0de7-49b3-8e33-d166d425eefd",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.13.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
